<?php
class Common_model extends CI_Model {

	var $error = '';
	function __construct()
    {
        parent::__construct();
    }

    public function set_error($e) {
    	$this->error = $e;
    }

    public function get_error() {
    	return $this->error;
    }

    public function log($level, $msg) {
    	log_message($level, $msg);
    }

    /**
     * 获取用于发post的tbs
     */
    public function get_tbs() {
        return $this->common->fetch_with_cookie("http://tieba.baidu.com/dc/common/tbs");
    }

    public function fetch($url, $conv = FALSE){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36');
        $data = curl_exec($curl);
        curl_close($curl);

        if ($conv) 
            $data = iconv("GBK", "UTF-8", $data);
        return $data;
    }

    public function fetch_with_cookie($url, $conv = FALSE) {
        $cookie = $_COOKIE;
        $cookie_str = '';
        foreach ($cookie as $key => $value) {
            $cookie_str .= "$key=$value; ";
        }
        $cookie_str = substr($cookie_str, 0, strlen($cookie_str) - 2);
        $cookie = $cookie_str;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
        $data   = curl_exec($curl);
        if ($conv) 
            $data = iconv("GBK", "UTF-8", $data);
        return $data;
    }

    public function fetch_with_post($url, $post = array(), $conv = FALSE, $UA = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36") {
        $cookie = $_COOKIE;
        $cookie_str = '';
        foreach ($cookie as $key => $value) {
            $cookie_str .= "$key=$value; ";
        }
        $cookie_str = substr($cookie_str, 0, strlen($cookie_str) - 2);
        $cookie = $cookie_str;

        $curlPost = '';
        foreach ($post AS $key => $value) {
            $curlPost .= "$key=" . urlencode($value) . "&";
        }
        $curlPost = substr($curlPost, 0, strlen($curlPost) - 1);

        echo $curlPost;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($curl, CURLOPT_USERAGENT, $UA);
        $data   = curl_exec($curl);
        if ($conv) 
            $data = iconv("GBK", "UTF-8", $data);
        echo $data;
        return $data;
    }
}

?>